package com.financing.platform.web.api;

import java.util.Collections;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.financing.platform.common.Pager;
import com.financing.platform.common.ResultCode;
import com.financing.platform.common.ResultMap;
import com.financing.platform.dto.XcxSearchnoRecordDTO;
import com.financing.platform.service.IXcxSearchnoRecordService;
import com.financing.platform.util.DTOdateSet;
@Controller("xcxSearchnoRecordController")
@RequestMapping("/api/xcxSearchnoRecord")
public class XcxSearchnoRecordController extends BaseAction  { 

	private Logger logger = Logger.getLogger(getClass());
	
	@Autowired
	private IXcxSearchnoRecordService xcxSearchnoRecordService;

	@RequestMapping("/listXcxSearchnoRecord")
	@ResponseBody
	public ResultMap list(HttpServletRequest request, XcxSearchnoRecordDTO xcxSearchnoRecord,Pager pager){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("query xcxSearchnoRecord list begin .......");
			
			xcxSearchnoRecord.setBegin(pager.getStartIndex());
			xcxSearchnoRecord.setPageSize(pager.getPageSize());
			
//			xcxSearchnoRecord.setFeedbackStatus("0");
			
			XcxSearchnoRecordDTO x = new XcxSearchnoRecordDTO();
		
			if (xcxSearchnoRecord.getFeedbackStatus() != null) {
				x.setFeedbackStatus(xcxSearchnoRecord.getFeedbackStatus());
			}
			
			
			int count = xcxSearchnoRecordService.queryAllList(x).size();
			
			pager.setTotalCount(count);
			
			List<XcxSearchnoRecordDTO> xcxSearchnoRecordList = null;
			
			if(count > 0){
				xcxSearchnoRecordList = xcxSearchnoRecordService.queryAllList(xcxSearchnoRecord);
			}else{
				xcxSearchnoRecordList = Collections.emptyList();
			}
			result.setAttribute("pager", pager);
			result.setAttribute("count",count);
			result.setAttribute("list", xcxSearchnoRecordList);
			result.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("query xcxNews list end .......");
		}catch(Exception e){
			log.error("query xcxNews error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}
		
		return result;
	}
	
	@RequestMapping("/listXcxSearchnoRecordUser")
	@ResponseBody
	public ResultMap listXcxSearchnoRecordUser(HttpServletRequest request, XcxSearchnoRecordDTO xcxSearchnoRecord){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("query xcxSearchnoRecord list begin .......");
			
			List<XcxSearchnoRecordDTO> xcxSearchnoRecordList = xcxSearchnoRecordService.queryList(xcxSearchnoRecord);
			result.setAttribute("list", xcxSearchnoRecordList);
			result.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("query xcxNews list end .......");
		}catch(Exception e){
			log.error("query xcxNews error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}
		
		return result;
	}
	
	@RequestMapping("/addXcxSearchnoRecord")
	@ResponseBody
	public ResultMap add(HttpServletRequest request, XcxSearchnoRecordDTO xcxSearchnoRecord){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("add xcxSearchnoRecord  begin .......");
			
			DTOdateSet.setCreateInfo(xcxSearchnoRecord,request.getSession());
			DTOdateSet.setUpdateInfo(xcxSearchnoRecord,request.getSession());
			
			int id = xcxSearchnoRecordService.insert(xcxSearchnoRecord);
			result.setAttribute("id", id);
			
			result.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("query xcxNews list end .......");
		}catch(Exception e){
			log.error("query xcxNews error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}
		
		return result;
	}
	
	
	@RequestMapping("/detailXcxSearchnoRecord")
	@ResponseBody
	public ResultMap detail(HttpServletRequest request, XcxSearchnoRecordDTO xcxSearchnoRecord){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("query detail xcxSearchnoRecord  begin .......");
			
			Integer id = xcxSearchnoRecord.getId();
			
			if(id==null){
				result.setStatusCode(ResultCode.STATUS_CODE_PARAMETERS_LOSE);
				result.setStatus(ResultMap.STATUS.FAILE);
				return result;
			}
			
			XcxSearchnoRecordDTO dto = xcxSearchnoRecordService.loadById(id);
			
			result.setAttribute("dto",dto);
			
			result.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("query xcxNews list end .......");
		}catch(Exception e){
			log.error("query xcxNews error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}
		
		return result;
	}
	
	@RequestMapping("/deleteXcxSearchnoRecord")
	@ResponseBody
	public ResultMap delete(HttpServletRequest request, XcxSearchnoRecordDTO xcxSearchnoRecord){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("delete xcxSearchnoRecord  begin .......");
			
			Integer id = xcxSearchnoRecord.getId();
			
			if(id==null){
				result.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
				result.setStatus(ResultMap.STATUS.FAILE);
				return result;
			}
			
			xcxSearchnoRecordService.delete(id);
			
			result.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("delete xcxSearchnoRecord  end .......");
		}catch(Exception e){
			log.error("delete xcxSearchnoRecord error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}
		
		return result;
	}
	
	@RequestMapping("/updateXcxSearchnoRecord")
	@ResponseBody
	public ResultMap update(HttpServletRequest request, XcxSearchnoRecordDTO xcxSearchnoRecord){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("update xcxSearchnoRecord  begin .......");
			
			Integer id = xcxSearchnoRecord.getId();
			
			if(id==null){
				result.setStatusCode(ResultCode.STATUS_CODE_PARAMETERS_LOSE);
				result.setStatus(ResultMap.STATUS.FAILE);
				return result;
			}
			
			DTOdateSet.setUpdateInfo(xcxSearchnoRecord,request.getSession());
			
			xcxSearchnoRecordService.update(xcxSearchnoRecord);
			
			result.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("update xcxSearchnoRecord list end .......");
		}catch(Exception e){
			log.error("update xcxSearchnoRecord error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}
		
		return result;
	}
	
	@RequestMapping("/updateForceXcxSearchnoRecord")
	@ResponseBody
	public ResultMap updateForce(HttpServletRequest request, XcxSearchnoRecordDTO xcxSearchnoRecord){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("updateForce xcxSearchnoRecord  begin .......");
			
			Integer id = xcxSearchnoRecord.getId();
			
			if(id==null){
				result.setStatusCode(ResultCode.STATUS_CODE_PARAMETERS_LOSE);
				result.setStatus(ResultMap.STATUS.FAILE);
				return result;
			}
			
			DTOdateSet.setUpdateInfo(xcxSearchnoRecord,request.getSession());
			
			xcxSearchnoRecordService.updateForce(xcxSearchnoRecord);
			
			
			result.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("updateForce xcxSearchnoRecord list end .......");
		}catch(Exception e){
			log.error("updateForce xcxSearchnoRecord error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}
		
		return result;
	}
	
}


//@Controller("xcxSearchnoRecordController")
//@RequestMapping("/api/xcxSearchnoRecord")
//public class XcxSearchnoRecordController extends BaseAction  { 
//
//	private Logger logger = Logger.getLogger(getClass());
//	
//	@Autowired
//	private IXcxSearchnoRecordService xcxSearchnoRecordService;
//	
//	@RequestMapping("/listXcxSearchnoRecord")
//	@ResponseBody
//	public ResultMap list(HttpServletRequest request, XcxSearchnoRecordDTO xcxSearchnoRecord,Pager pager){
//		ResultMap result = new ResultMap();
//		
//		try{
//			logger.info("query xcxSearchnoRecord list begin .......");
//			xcxSearchnoRecord.setBegin(pager.getStartIndex());
//			xcxSearchnoRecord.setPageSize(pager.getPageSize());
//			int count = xcxSearchnoRecordService.queryListCount(xcxSearchnoRecord);
//			pager.setTotalCount(count);
//			List<XcxSearchnoRecordDTO> xcxSearchnoRecordList = null;
//			if(count > 0){
//				xcxSearchnoRecordList = xcxSearchnoRecordService.queryList(xcxSearchnoRecord);
//			}else{
//				xcxSearchnoRecordList = Collections.emptyList();
//			}
//			result.setAttribute("pager", pager);
//			result.setAttribute("list", xcxSearchnoRecordList);
//			result.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
//			result.setStatus(ResultMap.STATUS.SUCCESS);
//			
//			logger.info("query xcxNews list end .......");
//		}catch(Exception e){
//			log.error("query xcxNews error!",e);
//			result.setStatus(ResultMap.STATUS.ERROR);
//			result.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
//		}
//		return result;
//	}
//}
